<?php

namespace App\Admin\Controllers;

//use AlibabaCloud\Client\Request\Request;
use App\Models\GoodsModel;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\AddressModel;
use App\Models\LineModel;
use App\Models\StoreModel;
use Illuminate\Http\Request;

class Goodscontroller extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '研学线路列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new GoodsModel());

        $grid->column('id', __('Id'));
        $grid->column('title','标题·');
        $grid->column('subheading','副标题');
       // $grid->column('content','内容');
        $grid->column('cove_img','封面图')->image('', 80, 80);
        //$grid->column('country_id', __('Country id'));
        //$grid->column('city_id', __('City id'));
        //$grid->column('line_id', __('Line id'));
        $grid->column('country','国家');
        $grid->column('line','游学线路类型');
       // $grid->column('city', __('City'));
        $grid->column('number','可登记人数');
        $grid->column('enrolment_number','登记人数');
        $grid->column('start_time', '开始时间');
        $grid->column('end_time','结束时间');
        $grid->column('price','价格');
        //$grid->column('price_info','收费说明');
       // $grid->column('store_id', __('Store id'));
        $grid->column('store_name','门店名称');
       // $grid->column('line_info','线路描述');
        //$grid->column('condition_info','学院条件');
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(GoodsModel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('title','标题');
        $show->field('subheading','副标题');
        $show->field('content', '内容');
        $show->field('cove_img','封面图')->image('',160,160);
        //$show->field('country_id', __('Country id'));
        //$show->field('city_id', __('City id'));
        //$show->field('line_id', __('Line id'));
        $show->field('country','国家');
        $show->field('line','游学线路类型');
       // $show->field('city', __('City'));
        $show->field('number','可登记人数');
        $show->field('enrolment_number','登记人数');
        $show->field('start_time','开始时间');
        $show->field('end_time','结束时间');
        $show->field('price','价格');
        $show->field('price_info','价格说明');
        //$show->field('store_id', __('Store id'));
        $show->field('store_name','门店名称');
        $show->field('line_info','线路描述');
        $show->field('condition_info','学院条件');
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));


        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new GoodsModel());

        $form->text('title','标题');
        $form->editor('subheading','副标题');
        $form->editor('content','内容');
        $form->image('cove_img','封面')->move('images/' . date('Ymd')) ->uniqueName();
        //$form->number('country_id', __('Country id'));
        $form->select('country_id','請選国家')
            ->options(AddressModel::where('parent_id',0)->pluck('address as title','id'))
            ->load('city_id', '/admin/city')
            ->rules('required',['required' => '請選擇国家']);

        $form->select('city_id','請選城市')
            //->options(AddressModel::where('parent_id',0)->pluck('address as title','id'))
            //->load('sort_id', '/admin/get_tow_sort')
            ->rules('required',['required' => '請選擇城市']);

        $form->select('line_id','請選线路类型')
            ->options(LineModel::pluck('line as title','id'))
            //->load('sort_id', '/admin/get_tow_sort')
            ->rules('required',['required' => '請選擇线路类型']);
       // $form->number('store_id', __('Store id'));
        $form->select('store_id','选择门店')
            ->options(StoreModel::pluck('store_name as title','id'))
            //->load('sort_id', '/admin/get_tow_sort')
            ->default(0);

        $form->number('number','可参团人数');
        $form->number('enrolment_number','登记人数');
        $form->datetime('start_time','开始时间')->default(date('Y-m-d H:i:s'));
        $form->datetime('end_time','结束时间')->default(date('Y-m-d H:i:s'));
        $form->decimal('price','价格')->default(0.00);
        $form->text('price_info','价格说明');
        $form->text('travel_time','行程时间');

        //$form->text('store_name', __('Store name'));
        $form->textarea('line_info','线路描述');
        $form->textarea('condition_info','学员条件')->default('空');

        $form->hidden('store_name','门店')->disable();
        $form->hidden('line','门店')->disable();
        $form->hidden('country','门店')->disable();
        $form->hidden('city','门店')->disable();

        $form->saving(function (Form $form) {
            if($form->store_id==0){
                $form->store_name ='平台';
                $form->store_id =  0;
            }else{
                $form->store_name =  StoreModel::where('id',$form->store_id)->value('store_name');
            }

            $form->line =  LineModel::where('id',$form->line_id)->value('line');

            $form->country =  AddressModel::where('id',$form->country_id)->value('address');

            $form->city =  AddressModel::where('id',$form->city_id)->value('address');

        });

        return $form;
    }

    //获取城市
    public function city(Request $request){
        $q =$request->input('q');

        $addressModel= new AddressModel();
        $query = $addressModel
            ->select([
                'id',
                'address as text'
            ]);

        $data = $query->where('parent_id',$q)->get()->toArray();

        return $data;

    }
}
